package com.qingmu.club.minaapi.custom.logic;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.qingmu.club.minaapi.plus.entity.ClubStaff;
import com.qingmu.club.minaapi.plus.entity.ClubUsers;
//import com.qingmu.club.minaapi.plus.entity.ExamItem;
import com.qingmu.club.minaapi.plus.entity.Users;
import com.qingmu.club.minaapi.plus.entity.UsersWx;
import com.qingmu.club.minaapi.plus.service.ClubStaffService;
import com.qingmu.club.minaapi.plus.service.ClubUsersService;
import com.qingmu.club.minaapi.plus.service.UsersService;
import com.qingmu.club.minaapi.plus.service.UsersWxService;
import com.qingmu.club.minaapi.utils.KeyValueDefine;
import com.qingmu.club.minaapi.utils.ResponseObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class UsersWxLogic {


    @Autowired
    private UsersWxService usersWxService;

    @Autowired
    private ClubUsersService clubUsersService;

    @Autowired
    private ClubStaffService clubStaffService;

    @Autowired
    private UsersService usersService;

    public ClubStaff getClubStaff(String staffId) {
        QueryWrapper<ClubStaff> qw = new QueryWrapper<>();
        qw.eq(ClubStaff.ID,staffId).eq(ClubStaff.STATUS,KeyValueDefine.ONLINE_STATUS);
        return this.clubStaffService.getOne(qw);
    }

    /**
     * 添加用户信息
     * @param nickName
     * @param userIcon
     * @param sex
     * @return
     */
    public Users addUserInfo(String nickName, String userIcon, String sex,String userId) {

        Users users = this.usersService.getById(userId);
        if(users==null){
            return users;
        }
        users.setUserSex(sex);
        users.setUserIcon(userIcon);
        users.setUserName(nickName);
        this.usersService.updateById(users);

        QueryWrapper<ClubUsers> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(ClubUsers.USER_ID,userId);
        List<ClubUsers> clubUsers = this.clubUsersService.list(queryWrapper);
        if(!CollectionUtils.isEmpty(clubUsers)){
            for(ClubUsers clubUsers1 : clubUsers){
                clubUsers1.setUserName(users.getUserName());
                clubUsers1.setUserSex(users.getUserSex());
                clubUsers1.setUserIcon(users.getUserIcon());
                clubUsers1.setUserMobile(users.getUserMobile());
                this.clubUsersService.updateById(clubUsers1);
            }
        }

        return users;
    }


    /**
     * 用户登录
     * @param openId
     * @param userMobile
     * @return
     */
    public Users loginByMobile(String openId, String userMobile) {

        QueryWrapper<UsersWx> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(UsersWx.OPEN_ID, openId);
        UsersWx u = this.usersWxService.getOne(queryWrapper);

        if (u != null) {
            Users users = this.usersService.getById(u.getUserId());
            return this.usersService.getById(users);
        }

        Users users = new Users();
        users.setUserMobile(userMobile);
        users.setUserShowStatus(KeyValueDefine.LATENT);//潜在学员
        this.usersService.save(users);

        UsersWx user = new UsersWx();
        user.setOpenId(openId);
        user.setType(KeyValueDefine.MENBER_MINI_APP);//会员端
        user.setUserId(users.getId());
        this.usersWxService.save(user);


        return this.usersService.getById(users.getId());
    }


}
